package com.tianlu.store.common;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.UUID;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
@Lazy(false)
public class IdGen {
    private static final String ROOT_NUMBER = "1234567890";
    private static final String ROOT_STR = "1234567890abcdefghijkmnpqrstuvwxyz";
    private static final String DATE_FROMAT_STR = "yyyyMMddHHmmss";
    private static final String UUID_SPLIT = "-";
    private static final int COUNT = 2;
    private static Snowflake snowflake = IdUtil.getSnowflake(getWorkId(), getDataCenterId());

    public IdGen() {
    }

    public static String uuidWorkCenter() {
        return UUID.randomUUID().toString();
    }

    public static String uuid() {
        return uuidWorkCenter().replaceAll("-", "");
    }

    public static String createRandomByTime(boolean numberFlag, int length) {
        Long time = System.currentTimeMillis() / 1000L;
        return time + createRandom(numberFlag, length);
    }

    public static String createRandomByDate(boolean numberFlag, int length) {
        return DateUtil.format(new Date(), "yyyyMMddHHmmss") + createRandom(numberFlag, length);
    }

    public static String createRandom(boolean numberFlag, int length) {
        StringBuilder retStr = new StringBuilder();
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;

        do {
            int count = 0;

            for(int i = 0; i < length; ++i) {
                double dblR = Math.random() * (double)len;
                int intR = (int)Math.floor(dblR);
                char c = strTable.charAt(intR);
                if ('0' <= c && c <= '9') {
                    ++count;
                }

                retStr.append(strTable.charAt(intR));
            }

            if (count >= 2) {
                bDone = false;
            }
        } while(bDone);

        return retStr.toString();
    }

    public static long snowflakeId() {
        return snowflake.nextId();
    }

    private static Long getWorkId() {
        try {
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            int[] ints = StringUtils.toCodePoints(hostAddress);
            int sums = 0;
            int[] var3 = ints;
            int var4 = ints.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                int b = var3[var5];
                sums += b;
            }

            return (long)(sums % 32);
        } catch (UnknownHostException var7) {
            return RandomUtils.nextLong(0L, 31L);
        }
    }

    private static Long getDataCenterId() {
        int[] ints = StringUtils.toCodePoints(SystemUtils.getHostName());
        int sums = 0;
        int[] var2 = ints;
        int var3 = ints.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            int i = var2[var4];
            sums += i;
        }

        return (long)(sums % 32);
    }
}
